package com.framework.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Component
public class PaddleHubServerEndConfig implements ApplicationListener<ContextClosedEvent> {

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        Thread paddleHubServerStartConfigEnd = new Thread(() -> {
            log.info("-------------------停止PaddleHub 服务---------------------------------");
            Runtime run = Runtime.getRuntime();
            try {
                Process paddleHubProcess = run.exec("hub serving stop --port 8866");
                InputStream in = paddleHubProcess.getInputStream();
                StringBuilder paddleResultInfo = new StringBuilder();
                while (in.read() != -1) {
                    paddleResultInfo.append(in.read());
                }
                in.close();
                log.info("PaddleHub 关闭成功" + paddleResultInfo.toString());
            } catch (IOException e) {
                log.error("PaddleHub 关闭失败：" + e.getMessage());
            }
        });
        paddleHubServerStartConfigEnd.start();
    }
}